package datacollection;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import bot.City;
import bot.DataStorage;
import bot.FileStorage;
import bot.Util;

public class DataFileConstruction {
	
	private static File demo = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\MarylandCensus.txt");
	private static File area = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\MarylandArea.txt");
	private static File links = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\MarylandLinks.txt");
	private static File output = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\MarylandData.txt");
	
	public static void main(String[] args){
		addExceptions();
//		try{
//			String toSkip[] = {};
//			BufferedReader linksReader = new BufferedReader(new FileReader(links));
//			String line = null;
//			DataStorage storage = new FileStorage(output);
//			while ((line = linksReader.readLine()) != null){
//				boolean skip = false;
//				for (String str: toSkip){
//					if (line.split(str).length > 1){
//						skip = true;
//						System.out.println("skipped " + line);
//					}
//				}
//				if (!skip){
//					try{
//						String placeName = Util.parseName(line);
//						boolean result = addPlace(line, placeName, storage);
//						if (!result){
//							System.out.println(line);
//						}
//					}
//					catch (StringIndexOutOfBoundsException e){
//						System.out.println(line);
//					}
//				}
//			}
//		} 
//		catch (FileNotFoundException e){
//			e.printStackTrace();
//		} 
//		catch (IOException e){
//			e.printStackTrace();
//		}
	}
	
	public static void addExceptions(){
		DataStorage storage = new FileStorage(output);
		addPlace("http://en.wikipedia.org/w/index.php?title=Saint_Michaels,_Maryland&action=edit", "St. Michaels", storage);
	}
	
	/**
	 * 
	 * @param line
	 * @param placeName
	 * @param storage
	 * @return true if there wasn't and issues attempting to add it false if not, note it is NOT true if it was added FALSE if not just
	 * weather it tried to add it or not, essential was storage.addEntry called.
	 */
	private static boolean addPlace(String line, String placeName, DataStorage storage){
		try{
			BufferedReader censusReader = new BufferedReader(new FileReader(demo));
			String demoLine = null;
			String demoParts[] = null;
			String areaLine = null;
			String areaParts[] = null;
			while ((demoLine = censusReader.readLine()) != null){
				String values[] = demoLine.split(" ;&: ");
				if (values.length > 31){
					throw new IllegalArgumentException("Split into too many parts");
				}
				if (values[0].equals(placeName)){
					demoParts = values;
					break;
				}
			}
			if (demoLine == null){
				return false;
			}
			censusReader.close();
			
			
			BufferedReader areaReader = new BufferedReader(new FileReader(area));
			while ((areaLine = areaReader.readLine()) != null){
				String values[] = areaLine.split(" ");
				String name = "";
				for (int i = 3; i < values.length - 7; i++){
					name += values[i] + " ";
				}
				if (name.substring(0, name.length() - 1).equals(placeName)){
					areaParts = values;
					break;
				}
			}
			//wasn't found in file
			if (areaLine == null){
				return false;
			}
			areaReader.close();
			
			if (demoParts != null && areaParts != null){
				City temp = new City.CityBuilder()
				.name(demoParts[0])
				.type(demoParts[1])
				.totalPopulation(demoParts[2])
				.percentUnder18(demoParts[3])
				.percent18to24(demoParts[4])
				.percent25to44(demoParts[5])
				.percent45to64(demoParts[6])
				.percentOver65(demoParts[7])
				.medianAge(demoParts[8])
				.percentMale(demoParts[9])
				.percentFemale(demoParts[10])
				.percentWhite(demoParts[11])
				.percentBlack(demoParts[12])
				.percentNative(demoParts[13])
				.percentAsian(demoParts[14])
				.percentPacific(demoParts[15])
				.percentOther(demoParts[16])
				.percentTwoOrMore(demoParts[17])
				.percentHispanic(demoParts[18])
				.households(demoParts[19])
				.families(demoParts[20])
				.percentHusbandAndWife(demoParts[21])
				.percentMaleNoWife(demoParts[22])
				.percentFemaleNoHusband(demoParts[23])
				.percentNonFamily(demoParts[24])
				.percentLivingAlone(demoParts[25])
				.percentLivingAloneOver65(demoParts[26])
				.percentWithChildren(demoParts[27])
				.averageHouseholdSize(demoParts[28])
				.averageFamilySize(demoParts[29])
				.numberOfHouses(demoParts[30])
				.areaLand(areaParts[areaParts.length - 4])
				.areaWater(areaParts[areaParts.length - 3])
				.populationEstimate2011(null)
				.sumlev(null)
				.state(null)
				.place(null)
				.build();
				storage.addEntry(line, temp);
			}
		}
		catch (IllegalArgumentException e){
			return false;
		}
		catch (IndexOutOfBoundsException e){
			return false;
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
		return true;
	}
	
}
